// +-----------------------+
// | 注释不留名，代码随便用 |
// +-----------------------+
define( function(){
    return  function(form){
        this.tags = 'input,textarea,select';
        var that = this;
        // 检测元素事件
        this.checkEvent = {change: true, blur: true, keyup: false};
        // 去除字符串两头的空格
        this.trim = function (str) {
            return str.replace(/(^\s*)|(\s*$)/g, '');
        };
        // 标签元素是否可见
        this.isVisible = function (ele) {
            return $(ele).is(':visible');
        };
        // 错误消息标签插入
        this.insertError = function (ele) {
            var $html = $('<span style="padding-right:12px;color:#a94442;position:absolute;right:0;font-size:12px;z-index:2;display:block;width:34px;text-align:center;pointer-events:none"></span>');
            $html.css({top: $(ele).position().top + 'px', paddingBottom: $(ele).css('paddingBottom'), lineHeight: $(ele).css('height')});
            $(ele).data('input-info') || $(ele).data('input-info', $html.insertAfter(ele));
        };
        this.isEmpty = function (ele, value) {
            var trim = this.trim(ele.value);
            value = value || ele.getAttribute('placeholder');
            return (trim === "" || trim === value);
        };
        this.isRegex = function (ele, regex, params) {
            var input = $(ele).val(), real = this.trim(input);
            regex = regex || ele.getAttribute('pattern');
            if (real === "" || !regex) return true;
            return new RegExp(regex, params || 'i').test(real);
        };
        this.checkAllInput = function () {
            var isPass = true;
            $(form).find(this.tags).each(function () {
                if (that.checkInput(this) === false) return $(this).focus(), isPass = false;
            });
            return isPass;
        };
        this.checkInput = function (input) {
            var tag = input.tagName.toLowerCase(), need = this.hasProp(input, "required");
            var type = (input.getAttribute("type") || '').replace(/\W+/, "").toLowerCase();
            if (this.hasProp(input, 'data-auto-none')) return true;
            var ingoreTags = ['select'], ingoreType = ['radio', 'checkbox', 'submit', 'reset', 'image', 'file', 'hidden'];
            for (var i in ingoreTags) if (tag === ingoreTags[i]) return true;
            for (var i in ingoreType) if (type === ingoreType[i]) return true;
            if (need && this.isEmpty(input)) return this.remind(input);
            return this.isRegex(input) ? (this.hideError(input), true) : this.remind(input);
        };
        this.hasProp = function (ele, prop) {
            if (typeof prop !== "string") return false;
            var attrProp = ele.getAttribute(prop);
            return (typeof attrProp !== 'undefined' && attrProp !== null && attrProp !== false);
        };
        // 验证标志
        this.remind = function (input) {
            if (!this.isVisible(input)) return true;
            this.showError(input, input.getAttribute('title') || input.getAttribute('placeholder') || '输入错误');
            return false;
        };
        // 错误消息显示
        this.showError = function (ele, content) {
            $(ele).addClass('validate-error'), this.insertError(ele);
            $($(ele).data('input-info')).addClass('layui-anim layui-anim-fadein').css({width: 'auto'}).html(content);
        };
        // 错误消息消除
        this.hideError = function (ele) {
            $(ele).removeClass('validate-error'), this.insertError(ele);
            $($(ele).data('input-info')).removeClass('layui-anim-fadein').css({width: '30px'}).html('');
        };

    }
});